<?php
	$TieuDe = $_POST['tieude'];
	$NoiDung = $_POST['noidung'];
	$nguoidung = new nguoidungbs ();
	$danhsach = $nguoidung->LayDanhSach ();
	$dem =0;
	$loi=false;
	$noidungloi="";
	include "../include/class.phpmailer.php";
	include "../include/class.smtp.php";
	$mail  = new PHPMailer();
	if($_FILES['TapTin']['size'] > 1048576) // 1MB * 1204KB * 1024bytes
	{
		echo "Chỉ cho phép upload tập tin từ 2MB trở xuống!";
		exit();
	}
	if($_FILES['TapTin']['error'] > 0 && $_FILES['TapTin']['error'] !=4)
	{
		echo "Lỗi tải tập tin lên : " . $_FILES['TapTin']['error'];
		exit();
	}
	else
	{
		$root_dir = "../data/";
		 
		$user_dir = "guiemailnhanvien/";
		 
		$full_dir = $root_dir . $user_dir;
		 
		if(!file_exists($full_dir))
		{
			mkdir($full_dir);
		}
		$file_name = "attach" . time() . rand(1,1000) . $_FILES['TapTin']['name'];
		 
		$file_path = $full_dir . $file_name;
	
		 
		if(move_uploaded_file($_FILES['TapTin']['tmp_name'], $file_path))
		{
			Header("Location: index.php?do=guiemailnhanvien&path="+ strval($file_path));
		}
		else
		{
			if($_FILES['TapTin']['name'] !="")
			{
				echo "Lỗi khi upload tập tin: " . $_FILES['TapTin']['error'];
				exit();
			}
		}
	}
	for($i = 0; $i < ($danhsach == false ? 0 : count($danhsach)); $i++)
	{
		if(isset($_POST[$danhsach[$i]["MaNguoiDung"]]))
		{
			$mail->IsSMTP();
			$mail->SMTPAuth  = true;                 #enable SMTP authentication
			$mail->SMTPSecure = "ssl";               #sets the prefix to the server
			$mail->CharSet="UTF-8";
			$mail->Host  = "smtp.gmail.com";         #sets GMAIL as the SMTP server
			$mail->Port       = 465;                 #set the SMTP port
			$mail->Username   = "heodatbietnoi1991";                  #your gmail username
			$mail->Password   = "thienthi90";                  #Your gmail password
			$mail->From       = "heodatbietnoi1991";                  #your gmail id
			$mail->FromName   = "Quỹ tín dụng Mỹ Hòa";                  #your name
			$mail->Subject    = $TieuDe;
			$mail->WordWrap   = 50;
			$mail->AddAddress($danhsach[$i]["Email"], $danhsach[$i]["Ho"]." ".$danhsach[$i]["Ten"]);
			$mail->IsHTML(true); // send as HTML
			$mail->Body = $NoiDung;
			if($_FILES['TapTin']['name'] !="")
				$mail->AddAttachment($file_path);
			if(!$mail->Send())
			{
				$loi=true;
				$noidungloi = $mail->ErrorInfo;
			}
			$dem=$dem+1;
		}
	}
	if($dem<1)
	{
		$_SESSION['GuiEmaiNhanVien'] = "Bạn chưa chọn nhân viên để gửi E-mail";
	}
	else if($loi==true)
	{
		$_SESSION['GuiEmaiNhanVien'] = "Lỗi khi gửi E-mail: "+$noidungloi;
	}
	else
	{
		$_SESSION['GuiEmaiNhanVien'] = "Đã gửi E-mail đến nhân viên";
		session_start();
		$nhatky = new nhatkybs();
		$nhatky->TenSuKien = "Gửi E-mail đến nhân viên";
		$nhatky->MaNguoiDung = $_SESSION["MaND"];
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$nhatky->ThoiGian = date("Y-m-d H:m:s", time());
		$nhatky->Them();
	}
	header("Location:index.php?do=guiemailnhanvien");
?>